package com.hnust.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cleveroom.core.CRMDevice;
import com.cleveroom.utils.Tools;
import com.hnust.entity.Device;
import com.hnust.entity.Position;
import com.hnust.listener.abstracts.DeviceListener;
import com.hnust.service.DeviceService;
import com.hnust.service.PositionService;
import com.hnust.utils.SimpleTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 创建人：zhangRuWang
 * 创建时间：2022/4/19
 * 描述：
 * 设备监听器，监听到对应设备后，将信息存储到数据库中
 */
@Component
public class CleveroomDeviceListener extends DeviceListener {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PositionService positionService;
    @Override
    protected void onDeviceChange(CRMDevice crmDevice) {
        //这里目前只收集设备的开关状态的变化
        String deviceUid=crmDevice.getUid();
        QueryWrapper<Device> deviceQuery=new QueryWrapper<>();
        deviceQuery.eq("uid",deviceUid);
        Device device=deviceService.getOne(deviceQuery);
        byte[] byte7s = Tools.byte2bit(crmDevice.getD7());
        device.setSwitches(byte7s[0]==1?1:0);
        deviceService.updateById(device);
    }
    @Override
    protected void onAddNewDevice(CRMDevice crmDevice) {
        String deviceUid=crmDevice.getUid();
        QueryWrapper<Device> deviceQuery=new QueryWrapper<>();
        deviceQuery.eq("uid",deviceUid);
        Device device=deviceService.getOne(deviceQuery);
        if(device!=null){
            //说明之前有保存到数据库中
            device.setState(1);//设置在线即可。
            deviceService.updateById(device);
        }else{
            //说明之前没有保存到数据库中,第一次接入设备
            //1.设置地理位置
            Position position=new Position();
            position.setRegionCode(0);position.setBuildCode(0);
            position.setFloorCode(crmDevice.getD3());
            position.setRoomCode(crmDevice.getD4());
            positionService.save(position);
            Device newDevice=new Device();
            newDevice.setPositionId(position.getId());
            //2.一些其它参数设置，具体看通讯协议14
            newDevice.setUid(crmDevice.getUid());
            newDevice.setCode(crmDevice.getD5());
            newDevice.setState(1);
            byte[] byte6s = Tools.byte2bit(crmDevice.getD6());
            byte[] byte7s = Tools.byte2bit(crmDevice.getD7());
            newDevice.setSwitches(byte7s[0]==1?1:0);
            newDevice.setDeviceType(byte7s[2]==1?"控制设备":"非控制设备");
            if(crmDevice.getD2()==200){
                //红外设备
                //设置红外设备哪些按键可用
                StringBuilder sb=new StringBuilder();
                for (byte byte6 : byte6s) {
                    sb.append(byte6).append(",");
                }
                for (byte byte7 : byte7s) {
                    sb.append(byte7).append(",");
                }
                newDevice.setInfraredGear(sb.toString());
            }
            newDevice.setDeviceIns(SimpleTools.crmDeviceToString(crmDevice));
            deviceService.save(newDevice);
        }
    }
}